package com.brave.shop.controller;

import com.brave.base.BaseController;
import com.brave.base.CheckNullParamKit;
import com.brave.base.RedisKit;
import com.brave.shop.dto.FindShopGoodsByTypeDTO;
import com.brave.shop.service.ShopQueryService;
import com.brave.user.dto.RedisUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: has-free-server-api
 * @PackageName: com.brave.shop.controller
 * @UserName: BraveKing-wy
 * @DateTime: 2019/10/6 21:47
 * @Description:
 **/
@Api(value = "商城部分", tags = "商城部分", description = "商城部分")
@RestController
@RequestMapping("/v1/shop")
public class ShopQueryController extends BaseController
{
    
    @Autowired
    private RedisKit redisKit;
    @Autowired
    private ShopQueryService shopQueryService;
    

    /**
     * 2019年10月6日 21:49:32
     * 查询出商城所有的种类
     * @return
     */
    @ApiOperation(value = "查询商城商品分类列表")
    @GetMapping(value = "/findShopTypeList", produces = "application/json")
    public String findShopTypeList(@RequestHeader String token)
    {
        //校验身份
        RedisUserInfo redisUserInfo = redisKit.checkUserToken(token);
        if(null == redisUserInfo)
        {
            return tokenBadRequest();
        }
        //调用逻辑
        return shopQueryService.findShopTypeList();
    }
    
    
    
    @ApiOperation(value = "查询商城类型下的商品列表")
    @ApiImplicitParam(name = "dto", value = "查询参数", required = true, dataType = "FindShopGoodsByTypeDTO")
    @PostMapping(value = "/findShopGoodsByType", produces = "application/json")
    public String findShopGoodsByType(@RequestHeader String token, @RequestBody FindShopGoodsByTypeDTO dto)
    {
        //校验身份
        RedisUserInfo redisUserInfo = redisKit.checkUserToken(token);
        if(null == redisUserInfo)
        {
            return tokenBadRequest();
        }
        //校验参数
        Map<String, String> checkMap = CheckNullParamKit.checkParam(dto);
        if(0 != checkMap.size())
        {
            return badParameter(checkMap);
        }
        //调用逻辑
        return shopQueryService.findShopGoodsByType(dto);
    }
    
    
    @ApiOperation(value = "查询商城商品的详细信息")
    @ApiImplicitParam(name = "goodsId", value = "商品编码", required = true, dataType = "long", paramType = "query")
    @GetMapping(value = "/findShopGoodsDetailInfo", produces = "application/json")
    public String findShopGoodsDetailInfo(@RequestHeader String token, @RequestParam("goodsId") long goodsId)
    {
        //校验身份
        RedisUserInfo redisUserInfo = redisKit.checkUserToken(token);
        if(null == redisUserInfo)
        {
            return tokenBadRequest();
        }
        //校验参数
        if(0 == goodsId)
        {
            Map<String, String> checkMap = new HashMap<>(2);
            checkMap.put("goodsId", String.format("%s:%s", "goodsId", "参数不可为空."));
            return badParameter(checkMap);
        }
        //调用业务逻辑
        return shopQueryService.findShopGoodsDetailInfo(goodsId);
    }
    
    
    
    
}